package cc.lzmsh.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * Author:   msh
 * Date:     2024/9/26 下午4:48
 * Description:
 */


public class AliOssUtil {
    private static final String ENDPOINT = "http://oss-cn-guangzhou.aliyuncs.com";
    private static final String ACCESS_KEY_ID = "LTAI5tR6Pn6V8Jwsdc5v9JPo";
    private static final String ACCESS_KEY_SECRET = "jgzr2RSPSEbXFYFbejDz2UhbFJkE8D";
    private static final String BUCKET_NAME = "sky-glutnn";

    public static String upload(String fileName, InputStream in) {
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        String url;

        try {
            ossClient.putObject(BUCKET_NAME, fileName, in);
            url = "https://" + BUCKET_NAME + "." + ENDPOINT.substring(ENDPOINT.lastIndexOf("/") + 1) + "/" + fileName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return url;
    }
}
